package com.talent.study.http;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: hszhou
 * @Date: 2025/6/5 15:50
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class Demo {

    public static void main(String[] args) {


        String imageUrl = "https://webdata.cqccms.com.cn/webdata/ImagepassController/imagePass.do";

        HashMap<String,String> params1 = new HashMap<>();
         doGet(params1,imageUrl);

        String url = "https://webdata.cqccms.com.cn/webdata/query/ZYCerti.do";
        HashMap<String,String> params = new HashMap<>();
        params.put("pageNumber","1");
        params.put("pageSize","100");
        params.put("manuname","博兰得");
        params.put("imgPwd","7");

        doPostForm( params, url);
    }
    /**
     * 发送 POST FROM 请求
     *
     * @param params 请求参数
     * @param url 请求地址
     * @return 响应状态码
     */
    private static int doPostForm(HashMap<String, String> params, String url) {

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            // 创建带参数的 GET 请求
            HttpPost request = new HttpPost(url);
            // 添加表单参数
            List<NameValuePair> paramsRequest = new ArrayList<>();
            params.forEach((key, value) -> paramsRequest.add(new BasicNameValuePair(key, value)));
            request.setEntity(new UrlEncodedFormEntity(paramsRequest, StandardCharsets.UTF_8));
            try (CloseableHttpResponse response = client.execute(request)) {
                String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
              //  log.info("POST请求状态码: {}", statusCode);
              //  return responseBody;

                log.info("[{}]",responseBody);
                return response.getStatusLine().getStatusCode();
            }
        }
        catch (Exception e) {
            log.error("authentication http 【{}】 is error", url, e);
            return -1;
        }
    }

    /**
     * GET请求
     *
     * @param params 请求参数
     * @param url 请求地址
     * @return 响应状态码
     */
    private static  int doGet(HashMap<String, String> params, String url) {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            URIBuilder uriBuilder = new URIBuilder(url);
            // 添加参数
            params.forEach(uriBuilder::addParameter);
            // 构建最终的 URI
            URI uri = uriBuilder.build();
            // 创建带参数的 GET 请求
            HttpGet request = new HttpGet(uri);
            try (CloseableHttpResponse response = client.execute(request)) {
                return response.getStatusLine().getStatusCode();
            }
        }
        catch (Exception e) {
            log.error("authentication http 【{}】 is error", url, e);
            return -1;
        }
    }
}
